﻿using System;
using System.Collections.Generic;
using SecurityLog.Models;
using SecurityLog.Repositories;

namespace SecurityLog.UnitTests.RepositoryStubs
{
    public class UserRepostiryStub : IUsersRepository 
    {
        private int _index;
        private readonly Dictionary<int, User> _storage = new Dictionary<int, User>();

        public User CreateNewUser(string name, string initials, string password, bool enabled)
        {
            var user = new User
                           {
                               Enabled = enabled,
                               Id = _index,
                               Initials = initials,
                               Name = name,
                               Password = password,
                               Entries = null
                           };
            _storage.Add(_index, user);
            _index++;
            return user;
        }

        public User EditUser(int id, string name, string initials, string password, bool enabled)
        {
            var user = new User
            {
                Enabled = enabled,
                Initials = initials,
                Name = name,
                Password = password,
                Entries = null
            };

            _storage[id] = user;
            return user;
        }

        public User GetUserById(int id)
        {
            return _storage[id];
        }

        public IEnumerable<User> GetAllUsers()
        {
            return _storage.Values;
        }

        public void DeleteUser(int id)
        {
            _storage.Remove(id);
        }

        public void ToggleUser(int id)
        {
            _storage[id].Enabled = !_storage[id].Enabled;
        }

        public bool IsEnabled(int id)
        {
            return _storage[id].Enabled;
        }

        public bool Validate(string initials, string password)
        {
            throw new NotImplementedException();
        }

        public User GetUser(string initials, string password)
        {
            foreach (var user in _storage.Values)
            {
                if (user.Initials == initials && user.Password == password)
                    return user;
            }
            return null;
        }
    }
}
